from ApplicationCommand import ApplicationCommand
from argument import *

from pyrfid.asn1.Encoder import Encoder

class ModifyObjectCommand(ApplicationCommand):
    """docstring for ModifyObjectCommand"""
    def __init__(self, tagId, objectIdentifier, objectValue, compactParameter, objectLock):
        
                                
        self.COMMAND_MODULE_OID = '28FC597E06'
        self.RESPONSE_MODULE_OID = '28FC597F06'
        self.ADD_TAG_ID = tagId
        self.ADD_OBJECT_IDENTIFIER = objectIdentifier
        self.ADD_OBJECT = objectValue
        self.ADD_COMPACT_PARAMETER = compactParameter
        self.ADD_OBJECT_LOCK = objectValue

        self.listCmdAndData = []
        self.ENCODED_COMMAND = ''
    
    def encode(self):
        """docstring for encode"""
  
        encoder = Encoder()
        
        addObjectLength = 0
        
        self.listCmdAndData.append(['06', '05', self.COMMAND_MODULE_OID])
        self.listCmdAndData.append(['30', '', ''])
     
        #tagID
        lenTagIDHex = encoder.lengthEncode(len(self.ADD_TAG_ID) / 2)
        self.listCmdAndData.append([self.OCTET_STRING, lenTagIDHex, self.ADD_TAG_ID])
     
        #OBJECT_IDENTIFIER(OBJECT IDENTIFIER)
        objectIdentifier = encoder.oidEncode(self.ADD_OBJECT_IDENTIFIER)
        lenOIDHex = encoder.lengthEncode(len(objectIdentifier) / 2)
        self.listCmdAndData.append([self.OBJECT_IDENTIFIER, lenOIDHex, objectIdentifier])
        addObjectLength += int(lenOIDHex, 16)
         
        #OBJECT(OCTET STRING)
        objectHex = encoder.octetStrEncode(self.ADD_OBJECT)
        lenObjHex = encoder.lengthEncode(len(objectHex) / 2)
        self.listCmdAndData.append([self.OCTET_STRING, lenObjHex, objectHex])
        addObjectLength += int(lenObjHex, 16)
      
        #COMPACT PARAMETER(INTEGER)
        self.listCmdAndData.append([self.INTEGER, '01', encoder.intEncode(self.ADD_COMPACT_PARAMETER)])
        addObjectLength += 1
         
        #OBJECT LOCK(BOOLEAN)
        self.listCmdAndData.append([self.BOOLEAN, '01', encoder.boolEncode(self.ADD_OBJECT_LOCK)])
        addObjectLength += 1
     
        #for TL : set length
        self.listCmdAndData[1][1] = str(int(lenTagIDHex, 16) + 1 + addObjectLength )

        self._convertToHexString()
        print self.listCmdAndData
        return self.ENCODED_COMMAND
     
 
    def decode(self):
        """docstring for decode"""
        pass
        
if __name__ == '__main__':
    
    tagId = "E200C0D2B25FD380"
    objectIdentifier = '1 0 15961 12 28'
    objectValue = 'WORLD'
    compactParameter = 1
    objectLock = False
        
    modifyObject = ModifyObjectCommand(tagId, objectIdentifier, objectValue, compactParameter, objectValue)
    print modifyObject.encode()